package com.nifostasky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.dao.SysUserDao;
import com.nifostasky.entity.*;
import com.nifostasky.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统用户(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2022-03-18 14:14:27
 */
@Primary
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> queryAllPerms(Long userId) {
        //获取用户的所有角色
        List<SysUserRole> userRoleList = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda()
                .eq(SysUserRole::getUserId, userId)
        );
        List<Long> roleIdList = userRoleList.stream().map(i -> i.getId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(roleIdList))
            return new ArrayList<>();
        //获取用户角色
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().lambda()
                .eq(SysRoleMenu::getRoleId, roleIdList)
        );
        List<Long> menuIdList = roleMenuList.stream().map(i -> i.getMenuId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(menuIdList))
            return new ArrayList<>();
        //获取所有角色权限
        List<SysMenu> sysMenus = sysMenuService.listByIds(menuIdList);
        return sysMenus;
    }
}
